const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const moment = require("moment");
const app = express();

//设置允许跨域访问该服务.
app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "*");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get("/getWeiboHot", function (req, res) {
  res.end(fs.readFileSync("./DataBase/hotSearch.json", "utf-8"));
});
app.get("/getDailyFXJson", function (req, res) {
  res.end(fs.readFileSync("./DataBase/DailyhuilvSearch.json", "utf-8"));
});
app.get("/getexchangeCNY", function (req, res) {
  let serverTime = moment(Date.now()).format("YYYY-MM-DD");
  let fileName = "./DataBase/exchange/exchangeLCNY" + serverTime + ".json";
  res.end(fs.readFileSync(fileName, "utf-8"));
});
app.get("/getWorld", function (req, res) {
  res.end(fs.readFileSync("./DataBase/world.json", "utf-8"));
});

app.listen(3000);
console.log("127.0.0.1:3000 已运行");

//微博爬虫模块
var weiBoHot = require("./newsServer/weiBoHot.js");
weiBoHot = weiBoHot.weiBoHot;
//weiBoHot();

//汇率爬虫模块(旧)
var exchangeRange = require("./newsServer/exchangeRange");
exchangeRange = exchangeRange.exchangeRange;
//exchangeRange();

//汇率爬虫模块(新)
var exchange = require("./newsServer/exchange");
exchange = exchange.exchange;
exchange();
